package com.qy.changemachine.util;

public class VersionUtils {

    /**
     * 比较两个版本号
     * @return 1: version1 > version2, -1: version1 < version2, 0: version1 == version2
     * @param version1 服务器 版本
     * @param version2 OS 版本
     */
    public static int compareVersions(String version1, String version2) {
        String[] parts1 = version1.split("\\.");
        String[] parts2 = version2.split("\\.");

        int length = Math.max(parts1.length, parts2.length);
        for (int i = 0; i < length; i++) {
            int v1 = i < parts1.length ? Integer.parseInt(parts1[i]) : 0;
            int v2 = i < parts2.length ? Integer.parseInt(parts2[i]) : 0;

            if (v1 > v2) return 1;
            if (v1 < v2) return -1;
        }
        return 0;
    }

    public static void main(String[] args) {
        String version1 = "1.9.1";
        String version2 = "1.10.1";

        int result = compareVersions(version1, version2);
        if (result > 0) {
            System.out.println(version1 + " is newer than " + version2);
        } else if (result < 0) {
            System.out.println(version1 + " is older than " + version2);
        } else {
            System.out.println(version1 + " is the same as " + version2);
        }
    }
}